package com.bilibili.juc.cf.one;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class FutureAllOfTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<Void> a = CompletableFuture.runAsync(()->{
            System.out.println(Thread.currentThread().getName()+"\t -----A come in " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("A我执行完了");
            System.out.println(Thread.currentThread().getName()+"\t -----A over " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        });
        CompletableFuture<Void> b = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName()+"\t -----B come in " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("B我执行完了");
            System.out.println(Thread.currentThread().getName()+"\t -----B over " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        });
        CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(a, b).whenComplete((m,k)->{
            System.out.println("A and B all finish");
            System.out.println(Thread.currentThread().getName()+"\t -----allOf whenComplete " + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        });
        System.out.println("all==> " + allOfFuture.get());
    }
}
/*
ForkJoinPool.commonPool-worker-2	 -----B come in 2024-11-21 15:36:04
ForkJoinPool.commonPool-worker-1	 -----A come in 2024-11-21 15:36:04
B我执行完了
ForkJoinPool.commonPool-worker-2	 -----B over 2024-11-21 15:36:05
A我执行完了
ForkJoinPool.commonPool-worker-1	 -----A over 2024-11-21 15:36:06
A and B all finish
ForkJoinPool.commonPool-worker-1	 -----allOf whenComplete 2024-11-21 15:36:06
all==> null
 */
